package com.xxx.meter.ui.meter.switchgear.home;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.xxx.meter.R;
import com.xxx.meter.base.BaseFragment;
import com.xxx.meter.model.data.swt.SwitchEntry;
import com.xxx.meter.model.http.bean.MachineChildBean;
import com.xxx.meter.model.utils.ButtonUtil;
import com.xxx.meter.model.utils.ToastUtil;
import com.xxx.meter.ui.meter.switchgear.info.MeterSwitchgearInfoActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MeterSwitchgearFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.OnItemClickListener {

    @BindView(R.id.main_recycler)
    RecyclerView mRecycler;
    @BindView(R.id.main_refresh)
    SwipeRefreshLayout mRefresh;
    @BindView(R.id.not_data)
    ImageView mNotData;

    private MeterSwitchgearAdapter mAdapter;
    private ArrayList<MachineChildBean> mList = new ArrayList<>();

    public static MeterSwitchgearFragment getInstance() {
        return new MeterSwitchgearFragment();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_switch;
    }

    @Override
    protected void initData() {
        mAdapter = new MeterSwitchgearAdapter(mList);
        mRecycler.setLayoutManager(new GridLayoutManager(getContext(), 3));
        mRecycler.setAdapter(mAdapter);
        mRefresh.setOnRefreshListener(this);
        mAdapter.setOnItemClickListener(this);
    }

    @OnClick({R.id.not_data})
    public void OnClick(View view) {
        if (ButtonUtil.isFastClick()) return;
        switch (view.getId()) {
            case R.id.not_data:
                ToastUtil.showToast("暂无数据");
                break;
        }
    }

    @Override
    public void onRefresh() {
        mRefresh.setRefreshing(false);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        if (ButtonUtil.isFastClick()) return;
        MachineChildBean machineBean = mList.get(position);
        Intent intent = new Intent(getContext(), MeterSwitchgearInfoActivity.class);
        intent.putExtra("machineId", machineBean.getId());
        intent.putExtra("machineType", machineBean.getType());
        intent.putExtra("machineName", machineBean.getName());
        startActivityForResult(intent, 1);
    }

    //加载数据
    public void loadMachineData(List<MachineChildBean> list) {
        if (list == null || list.size() == 0) {
            mNotData.setVisibility(View.VISIBLE);
            mRefresh.setVisibility(View.GONE);
        } else {
            mNotData.setVisibility(View.GONE);
            mRefresh.setVisibility(View.VISIBLE);
            mList.clear();
            mList.addAll(list);
            mAdapter.notifyDataSetChanged();
            mRefresh.setRefreshing(false);
        }
    }

    public void refresh(List<SwitchEntry.DataItemEntry> list) {
        mAdapter.setList(list);
    }

}
